package com.baiyun.yunpan.Util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * @author Sam
 * @date 2021/4/25 22:21
 */
public class JWTUtils {
    private static final  String SING = "N$hr!v^*nok7lFbBWiwgKR4spgdvmncUhAPA$dA6Y8*y744e#Fo0lppkOfbgvpuBIgv6y8zJ$X@JufAkuIBo1mJi3Uw%bdwlcBc";

    /**
     * 生成token
     * @param map
     * @return
     */
    public static  String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,1);
        //创建jwt builder
        JWTCreator.Builder builder = JWT.create();

        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SING));
         return token;
    }

    public  static DecodedJWT verify(String token){
            return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }


}
